package servlet.stu;

import bean.Public;
import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


//学生获取某楼栋的某一名称的公共设施情况
@WebServlet("/stu/getPublicOneName")
public class GetPublicOneNameServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
        String publicName=request.getParameter("publicName");
        String user_token = request.getHeader("user_token_header");
        Integer buildId = JwtUtils.getStuBuildIdFromToken(user_token,"buildId");
        List<Public> publicList= UserDaoImpl.getPublicOneNameByStu(currentPage,publicName,buildId);
        Result result = new Result();
        if(publicList!=null){
            result.success(publicList);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
